package example.controller;

//import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import example.config.MyWebAppConfigurer;
import example.dao.Result;
import example.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.io.*;
import java.util.List;
import java.util.UUID;

/**
 * 文件上传
 */
@Controller
@RequestMapping({"/file"})
public class FileController {

//    @GetMapping(value = "/file")
//    public String file() {
//        return "file";
//    }
@Autowired()
private FileService fileService;

    @PostMapping(value = "/fileUpload")
    @ResponseBody
    public Result fileUpload(Model model, HttpServletRequest req, HttpSession session) throws IOException {
        String filename=fileService.upLoadFile(req);

        Result result = new Result();
        if (filename.equals("")){
            result.setResult("error");
            result.setMessage("上传失败");
        }else{
            result.setResult("success");
            result.setMessage("上传完成");

        }
        result.setImgUrl(filename);

        return result;
//        model.addAttribute("filename", filename);
//        return "file";
    }
}